'use strict'
import React, {
    Component
} from 'react'
import {
    Link,
    hashHistory
} from 'react-router'

import Header from './Common/Header'
import {
    HttpService,
    dateStrFormat,
    Tips,
    getClassList
} from '../Tools'

export default class Login extends Component {
    constructor(props) {
        super(props)
        this.state = {
            loginUsers: []
        }
    }

    componentWillMount() {
        sessionStorage.clear()
        sessionStorage.clear()
    }
    componentDidMount() {
        //this.refs.passid.value = 18158530643
        this.refs.passid.value = 15305712669
        //this.refs.passid.value = 13980604493
        this.refs.userpwd.value = 123456
    }

    //登录
    Login(e) {
        e.preventDefault();
        const passid = this.refs.passid.value,
            userpwd = this.refs.userpwd.value;
        if (userpwd == '' || passid == '') {
            Tips('请填写用户名和密码')
            return;
        }
        HttpService.query({
            url: '/jxt-commonbackground/ajaxLoginSystem.do',
            data: {
                passid,
                userpwd,
                method: 'ajaxJybJSON_002PassLogin'
            },
            success: (res => {
                if (res.code == 0) {
                    sessionStorage.ticket = res.msg
                    this.setState({
                        loginUsers: res.rs
                    })
                    Tips('选择身份进入', 1);
                } else {
                    Tips(res.msg);
                }
            })
        })
    }

    //登录第二步
    LoginSecond(data, userrole, orgid, orgname, userid, username) {
        HttpService.query({
            url: '/jxt-commonbackground/ajaxLoginSystem.do?' + data,
            success: (res => {
                if (res.code == 0) {
                    sessionStorage.userrole = userrole
                    sessionStorage.orgid = orgid
                    sessionStorage.orgname = orgname
                    sessionStorage.userid = userid
                    sessionStorage.username = username
                    sessionStorage.ticket = res.r.ticket
                    getClassList((classList) => {
                        if (classList.length == 1) {
                            sessionStorage.classid = classList[0].id;
                            sessionStorage.classname = classList[0].name
                        }
                        hashHistory.push('/index')
                    })
                }
            })
        })
    }
    render() {
        const loginRole = this.state.loginUsers.map((item, index) => {
            let data = `key=${sessionStorage.ticket}&method=ajaxJybJSON_011SelectLoginOrgAndUser&orgid=${item.orgid}&userid=${item.userid}`
            return (
                <li data={data} onClick={this.LoginSecond.bind(this, data, item.userrole, item.orgid, item.orgname, item.userid, item.username)}>{item.userrole}
                    ： {item.username}
                    ({item.orgname})
                </li>
            )
        })
        return (
            <div>
                <Header title='登录页面'/>
                <form className='loginbox' onSubmit={this.Login.bind(this)} id='loginform' action=''>
                    <input type='text' className='hidden' name='method' value='ajaxJybJSON_002PassLogin'/><br/>
                    <input type='tel' placeholder='手机号' name='passid' ref='passid' maxLength='11' /><br/>
                    <input type='password' placeholder='密码' name='userpwd' ref='userpwd'/><br/>
                    <input type='submit' className='sumbmit' value='提交'/>
                    <input type='reset' className='reset' value='重置'/>
                    <ul id='loginrole'>
                        {loginRole}
                    </ul>
                </form>

            </div>
        )
    }
}
